package com.example.club_mvvm.base

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/**
 * @author yaotianxue
 * @description app所有扩渣函数封装在此
 * @date 2025/1/11
 */


//扩展函数：在不修改原有String类基础上,为String类扩展a方法
fun String.a():String{
    return this+"aaa"
}


//扩展函数：为Flow流扩展
fun <T> Flow<T>.observeWithLifecycle(
    lifecycleOwner: LifecycleOwner,
    minState: Lifecycle.State = Lifecycle.State.STARTED,
    action: suspend (T)->Unit
): Job = lifecycleOwner.lifecycleScope.launch {
//    lifecycleOwner.repeatOnLifecycle(minState){
//        collectLatest {
//            action.invoke(it)
//        }
//    }
    flowWithLifecycle(lifecycleOwner.lifecycle,minState).collectLatest {
        action.invoke(it)
    }
}